 <!DOCTYPE html>
 
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="java.util.Date" %> 
<%@ page import="java.text.DateFormat" %> 
<%@ page import="java.text.SimpleDateFormat" %> 

<% 
try {
	
	String connectionURL = "jdbc:mysql://localhost:3306/unilearn"; 
	
	int c_row_count = 0;
	Connection connection = null;
	Statement stmt = null;
	ResultSet rs=null;

	Class.forName("com.mysql.jdbc.Driver").newInstance(); 

	connection = DriverManager.getConnection(connectionURL, "root", "");
	stmt = connection.createStatement();
	
	if(!connection.isClosed())
	{
		
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		Date _now = new Date();
		
		//get session values
		String user_id = (String)session.getAttribute("UserID");
		String permission = (String)session.getAttribute("Permission");
		String first_name = (String)session.getAttribute("LastName");
		String last_name = (String)session.getAttribute("FirstName");
		String class_group_id = (String)session.getAttribute("ClassGroupID");
	
%>

<html lang="en">
<head>
<script type="text/javascript">
	function getlatePenalty(_value)
	{
		//var _chk = document.getElementsByName("datLatePenalty");
	
		if(_value.checked == true)
		{
			document.getElementsByName("datLatePenalty")[0].value = "1";
		}else{
			document.getElementsByName("datLatePenalty")[0].value = "0";
		}
	}
	
	function setAction(_value)
	{
		if(_value == 1)
		{
			document.getElementsByName("datAction")[0].value = "1";
			
		}else{
		
			document.getElementsByName("datAction")[0].value = "0";
		}
	}
	
	function setAssignmentID(ass_id)
	{
		document.getElementsByName("datAssignmnetID")[0].value = ass_id;
	}
</script>
<link rel="stylesheet" type="text/css" href="common.css">
<title>Assignmenr Update</title>
</head>
<body>
<form action="assignmentUpdate.jsp" method="post" id="assignCreateFrom" name="entry" onSubmit="return validate(this)">
<table class="TblBody" style="width:720px">
	<tr>
		<td>
			<div class="contentb">
				<table width="100%">
                    <tr>
                        <td style="background-color:#ccff99; font-size:20px; font-weight:bold; height:30px">Assignment Create</td>
                    </tr>
					<tr>
                        <td>
							<%
								String str_assignment_id = request.getParameter("AssignmentID");
								rs = stmt.executeQuery("SELECT * FROM assignment WHERE assignment_id = " + str_assignment_id);
								
								while(rs.next())
								{
							%>
								<table width="100%">
									<tr>
										<td style="width:16%">Assignment Name</td>
										<td>	
											<input type="hidden" name="datAssignmnetID" />
											<input type="hidden" name="datAction" />
											<!--<input name="txtAssignmentName" type="text" />-->
											<%= rs.getString("assignment_name") %>
										</td>
									</tr>
									<tr>
										<td>End Date</td>
										<td>
											<select name="cboEndDateDD">
												<option value="1">01</option>
												<option value="2">02</option>
												<option value="3">03</option>
												<option value="4">04</option>
												<option value="5">05</option>
												<option value="6">06</option>
												<option value="7">07</option>
												<option value="8">08</option>
												<option value="9">09</option>
												<option value="10">10</option>
												<option value="11">11</option>
												<option value="12">12</option>
												<option value="13">13</option>
												<option value="14">14</option>
												<option value="15">15</option>
												<option value="16">16</option>
												<option value="17">17</option>
												<option value="18">18</option>
												<option value="19">19</option>
												<option value="20">20</option>
												<option value="21">21</option>
												<option value="22">22</option>
												<option value="23">23</option>
												<option value="24">24</option>
												<option value="25">25</option>
												<option value="26">26</option>
												<option value="27">27</option>
												<option value="28">28</option>
												<option value="29">29</option>
												<option value="30">30</option>
												<option value="31">31</option>
											</select>
											&nbsp; / &nbsp;
											<select name="cboEndDateMM">
												<option value="1">01</option>
												<option value="2">02</option>
												<option value="3">03</option>
												<option value="4">04</option>
												<option value="5">05</option>
												<option value="6">06</option>
												<option value="7">07</option>
												<option value="8">08</option>
												<option value="9">09</option>
												<option value="10">10</option>
												<option value="11">11</option>
												<option value="12">12</option>
											</select>
											&nbsp; / &nbsp;
											<select name="cboEndDateYY">
												<option value="2011">2011</option>
												<option value="2012">2012</option>
												<option value="2013">2013</option>
											</select>
										</td>
									</tr>
									<tr>
										<td>Assigment Status</td>
										<td>
											<%
												String ass_status = rs.getString("assignment_status");
												int int_ass_status = Integer.parseInt(ass_status);
												if(int_ass_status == 1)
												{
											%>
												Open
											<%
												}else{
											%>
												Close
											<%
												}
											%>
										</td>
									</tr>
									<tr>
										<td>Assignment Type</td>
										<td>
										<%
											String ass_type = rs.getString("assignment_type");
											String late_penalty_flag = rs.getString("late_penalty_flag");
											
											int int_ass_type = Integer.parseInt(ass_type);
											int int_late_penalty = Integer.parseInt(late_penalty_flag);
											
											if(int_ass_type == 1)
											{
										%>
											Local
										<%
											}else if(int_ass_type == 2){
										%>
											Team
										<%
											}
										%>
										 </td>
									</tr>
									<tr>
										<td>Late Penalty</td>
										<td>
											<input type="hidden" name="datLatePenalty" value="0" >
											<%
												if (int_late_penalty == 0){
											%>
												<input type="checkbox" onClick="getlatePenalty(this)" name="chkLatePenalty" />
											<%}else{%>
												<input type="checkbox" onClick="getlatePenalty(this)" checked="true" name="chkLatePenalty" />
											<%}%>
										</td>
									</tr>
									<tr>
										<td>Specification</td>
										<td>
											<!-- <input name="spec_assignment" type="file" />-->
											<%= rs.getString("spec_file_path") %>
										</td>
									</tr>
									<tr>
										<td>Description</td>
										<td>
											<textarea name="txtdescription" value="<% out.println(rs.getString("description")); %>" cols="50" rows="7"></textarea>
										</td>
									</tr>
									<tr>
										<td colspan="2">
											<input id="btnSubmit" type="submit" onClick="setAction(1); setAssignmentID(<%= str_assignment_id %>)" value="Submit" />&nbsp;&nbsp;
											<input id="btnReset" type="reset" value="Reset" />
										</td>
									</tr>
								</table>
							<%
								}
							%>
                        </td>
					</tr>
				</table>
				<%
					
					String str_action_flag = request.getParameter("datAction");
					int int_action_flag = 0;
					if (str_action_flag != null && str_action_flag != ""){
						int_action_flag = Integer.parseInt(str_action_flag);
					}
					
					if(int_action_flag > 0){
					
						int result_row = 0;
						String e_dd = request.getParameter("cboEndDateDD");
						String e_mm = request.getParameter("cboEndDateMM");
						String e_yy = request.getParameter("cboEndDateYY");
						String late_penalty = request.getParameter("datLatePenalty");
						String desc = request.getParameter("txtdescription");
						String assignment_id = request.getParameter("datAssignmnetID");
						
						String full_e_date = "";
						if (e_dd != "" && e_mm != "" && e_yy != "")
						{
							full_e_date = e_yy + "-" + e_mm + "-" + e_dd + " 00:00:00";
						}
						
						//out.println("UPDATE assignment set end_date = '"+full_e_date+"', description = '"+desc+"', late_penalty_flag = '"+late_penalty+"', modify_date = '"+dateFormat.format(_now)+"'  WHERE assignment_id = " + assignment_id );
						result_row = stmt.executeUpdate("UPDATE assignment set end_date = '"+full_e_date+"', description = '"+desc+"', late_penalty_flag = '"+late_penalty+"', modify_date = '"+dateFormat.format(_now)+"' WHERE assignment_id = " + assignment_id ); 
							
						if (result_row > 0)
						{
							out.println( "<script>alert('Update Successful');window.location='createAssSubjectList.jsp'</script>" );
						}else{
							out.println( "<script>alert('Update Failed'); window.location='assignmentUpdate.jsp?AssignmentID="+assignment_id+"'</script>" );
						}
					}
				%>
			</div>
		</td>
	</tr>
</table>
</form>
<%
}
	}catch(Exception ex){

		out.println(ex);
	}
%>
</body>
</html>